package com.frame.database.proxy;

import java.lang.reflect.Method;
import com.frame.database.DataException;
import com.frame.database.operate.Jsession;
import com.frame.database.session.JdbcSession;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * @Title: 系统
 * @Description:
 * @Copyright: Copyright (c) 2012
 * @Company:
 * @author 王海锋
 * @CreatedTime:2012-6-9 下午12:58:00
 * @version 1.0
 */

public class JdbcProxy implements MethodInterceptor{

    private Jsession session;
    public JdbcProxy(){

    }

    public Object intercept(Object obj, Method method, Object[] pams, MethodProxy proxy) throws Throwable {
        // TODO Auto-generated method stub
        Object o=null;
        String methodName=method.getName();
        if("getSession".equals(methodName)){
            if(session==null){
                session=new JdbcSession();
            }
            return session;
        }else{
            try{
                o=proxy.invokeSuper(obj,pams);
            }
            catch(Exception e){
                e.printStackTrace();
                throw new DataException(e.getMessage(),e);
            }
            finally{
                if(session!=null){
                    session.closeSession();
                }

            }
        }
        return o;
    }
}
